package it.polimi.traveldream.ejb.dtocreator;

import java.util.ArrayList;

import java.util.List;

import it.polimi.traveldream.ejb.entities.Excursion;
import it.polimi.traveldream.ejb.entities.Hotel;
import it.polimi.traveldream.ejb.entities.Transport;
import it.polimi.traveldream.ejbclient.dtos.BaseProductDTO;
import it.polimi.traveldream.ejbclient.dtos.ExcursionDTO;
import it.polimi.traveldream.ejbclient.dtos.HotelDTO;
import it.polimi.traveldream.ejbclient.dtos.TransportDTO;

public class BaseProductDTOCreator {
	public static BaseProductDTO createBaseProduct(Object objectEntity) {
		if(objectEntity instanceof Excursion) {
			Excursion entity = (Excursion) objectEntity;
			ExcursionDTO excursion = new ExcursionDTO(entity.getId(),entity.getDescription(),
					entity.getEndTrip(), entity.getMaxAvailability(), entity.getOverpast(),
					entity.getPrice(), entity.getReservations(), entity.getStartTrip(),
					entity.getStartAddress(), entity.getOrganizer(), entity.getPathKm());
			return excursion;
		} else if (objectEntity instanceof Hotel) {
			Hotel entity = (Hotel) objectEntity;
			HotelDTO hotel = new HotelDTO(entity.getId(),entity.getDescription(),
					entity.getEndTrip(), entity.getMaxAvailability(), entity.getOverpast(),
					entity.getPrice(), entity.getReservations(), entity.getStartTrip(),
					entity.getAddress(), entity.getName(), entity.getRating());
			return hotel;
		} else if (objectEntity instanceof Transport) {
			Transport entity = (Transport) objectEntity;
			TransportDTO transport = new TransportDTO(entity.getId(),entity.getDescription(),
					entity.getEndTrip(), entity.getMaxAvailability(), entity.getOverpast(),
					entity.getPrice(), entity.getReservations(), entity.getStartTrip(),
					entity.getCompany(), entity.getDepartureCity(), entity.getArrivalCity());
			return transport;
		}
		return null;
	}
	
	public static List<? extends BaseProductDTO> createBaseProductList(List<? extends Object> objectEntities) {
		if(objectEntities != null && !objectEntities.isEmpty()) {
			if(objectEntities.get(0) instanceof Excursion) {
				List<ExcursionDTO> excursions = new ArrayList<ExcursionDTO>();
				for(Object entity : objectEntities) {
					excursions.add((ExcursionDTO) createBaseProduct(entity));
				}
				return excursions;
			} else if(objectEntities.get(0) instanceof Hotel) {
				List<HotelDTO> hotels = new ArrayList<HotelDTO>();
				for(Object entity : objectEntities) {
					hotels.add((HotelDTO) createBaseProduct(entity));
				}
				return hotels;
			} else if(objectEntities.get(0) instanceof Transport) {
				List<TransportDTO> transports = new ArrayList<TransportDTO>();
				for(Object entity : objectEntities) {
					transports.add((TransportDTO) createBaseProduct(entity));
				}
				return transports;
			}
		}
		return null;
	}
	
}
